<%@codepage="65001"%>
<%session.codepage="65001"%>
<!-- #include virtual=/inc/dbConn.asp -->
<!-- #include virtual=/inc/function.asp -->
<!-- #include virtual=/inc/action.header.inc.asp -->

<%
	'## 파라미터 변수 선언부
	stdMonth		= Request("findMonth")
	procType		= Request("findProcType")

	'Response.write procType

	If procType = "" Then procType = "10" End If 
	If stdMonth = "" Then  stdMonth = Date() End If 
	
	SQLTitle = "-- ■■■■ 달력 ■■■■"
	
	lastDate = getLastDay(stdMonth)

	Response.write "<Calendar>" & vbcrlf
		Call mkXMLNode("stdMonth", Left(stdMonth,7), 1)
		firstDate = Left(stdMonth,7) & "-01"
		firstDay = weekday(firstDate)
		nextMonth = dateadd("m",1,firstDate)
		nextMonth = Left(nextMonth,7)
		prevMonth = dateadd("m",-1,firstDate)
		prevMonth = Left(prevMonth,7)
		Call mkXMLNode("startDay", firstDay, 1)
		Call mkXMLNode("endDate", lastDate, 1)
		Call mkXMLNode("curDate", Date(), 1)
		Call mkXMLNode("nextMonth", nextMonth, 1)
		Call mkXMLNode("prevMonth", prevMonth, 1)
		curMonthBadRate = getBadRate(stdMonth)		
		prevMonthBadRate = getBadRate(prevMonth)
		Call mkXMLNode("RateCurMonth", curMonthBadRate, 1)
		Call mkXMLNode("RatePrevMonth", prevMonthBadRate, 1)
	
	If procType = "100" Then
		SQL = "SELECT B.srcNm, A.issuedMemo, A.issuedDt workDt, SUM(ISNULL(A.input,0)) ttlInput, SUM(ISNULL(A.output,0)) ttlOutput, B.srcSeq FROM tblSrcInOut A " & _
				" LEFT JOIN tblSrc B ON A.srcCd=B.srcCd " & _
				" WHERE A.issuedDt LIKE '" & Left(stdMonth,7) & "%'  AND A.flag='Y' GROUP BY B.srcNm, A.issuedMemo, A.issuedDt, B.srcSeq ORDER BY B.srcSeq"
	ElseIf procType = "offwork" Then 
		SQL = "SELECT B.userNm msg, A.workTm, A.workDt FROM tblOffDayWork A " & _
				" LEFT JOIN tblUser B ON A.userId=B.userId " & _
				" WHERE A.workDt LIKE '" & Left(stdMonth,7) & "%'  AND A.flag='Y' ORDER BY A.workDt"
	ElseIf procType = "extra" Then 
		SQL = "SELECT A.prdNm msg, A.flag, A.qty, A.workDt FROM tblExtraOrder A " & _
				" WHERE A.workDt LIKE '" & Left(stdMonth,7) & "%'  AND (A.flag='Y' OR A.flag='C') ORDER BY A.workDt"
	ElseIf procType = "equip" Then 
		SQL = " SELECT A.lotYear, A.lotNo, A.lotSubNo, A.etc msg, A.workDt, SUM(A.goodQty) goodQty, SUM(A.badQty) badQty, C.prdNm, B.type " & _
				" FROM tblOrderResult A " & _
				" LEFT JOIN tblOrder B ON A.lotYear=B.lotYear AND A.lotNo=B.lotNo AND A.lotSubNo=B.lotSubNo " & _
				" LEFT JOIN tblProduct C ON B.prdCd=C.prdCd " & _
				" WHERE A.type='10' AND A.flag='Y' AND B.flag<>'N' AND  A.workDt LIKE '" & Left(stdMonth,7) & "%' " & _
				" GROUP BY A.lotYear, A.lotNo, A.lotSubNo,A.etc, A.workDt, C.prdNm, B.type ORDER BY A.workDt,A.etc"
	ElseIf procType = "inventory" Then 
		SQL = " SELECT A.lotYear, A.lotNo, A.lotSubNo, A.etc, A.workDt, SUM(A.goodQty) goodQty, SUM(A.badQty) badQty, C.prdNm, B.type " & _
				" FROM tblOrderResult A " & _
				" LEFT JOIN tblOrder B ON A.lotYear=B.lotYear AND A.lotNo=B.lotNo AND A.lotSubNo=B.lotSubNo " & _
				" LEFT JOIN tblProduct C ON B.prdCd=C.prdCd " & _
				" WHERE A.type IN ('20','90') AND A.flag='Y' AND B.flag<>'N' AND  A.workDt LIKE '" & Left(stdMonth,7) & "%' " & _
				" GROUP BY A.lotYear, A.lotNo, A.lotSubNo, A.etc, A.workDt, C.prdNm, B.type ORDER BY A.etc, A.workDt"
	Else	
		SQL = " SELECT A.lotYear, A.lotNo, A.lotSubNo, A.etc, A.workDt, SUM(A.goodQty) goodQty, SUM(A.badQty) badQty, C.prdNm, B.type " & _
				" FROM tblOrderResult A " & _
				" LEFT JOIN tblOrder B ON A.lotYear=B.lotYear AND A.lotNo=B.lotNo AND A.lotSubNo=B.lotSubNo " & _
				" LEFT JOIN tblProduct C ON B.prdCd=C.prdCd " & _
				" WHERE A.type='" & procType & "' AND A.flag='Y' AND B.flag<>'N' AND  A.workDt LIKE '" & Left(stdMonth,7) & "%' " & _
				" GROUP BY A.lotYear, A.lotNo, A.lotSubNo, A.etc, A.workDt, C.prdNm, B.type ORDER BY A.etc, A.workDt"
	End If 

	If xMode="debug" Then 
		Call showDebug(SQLTitle,SQL) 
	End If 

	Set Rs=Server.CreateObject("ADODB.RecordSet")
	Rs.open SQL, DbCon, 1

	Response.write "	<Work>" & vbcrlf
	If Rs.eof Or Rs.bof Then 
	Else 
		Do Until Rs.eof Or Rs.bof
		
			idNm = "D" & Replace(rs("workDt"),"-","")
			If procType = "100" Then
				msg = "&nbsp; - " &  rs("srcNm")   '##rs("issuedMemo")
				If  rs("ttlOutput") > 0 Then 
					msg = msg & " : -" & rs("ttlOutput")
				End If
				If  rs("ttlInput") > 0 Then
					If  rs("ttlOutput") > 0 Then 
						msg = msg & " /"
					Else  
						msg = msg & " :"
					End If
					msg = msg & " +" & rs("ttlInput")
				End If
				msg = msg & "<br>"
			ElseIf procType = "extra" Then
			
				msg = rs("msg")
				if len(rs("msg")) > 25 then 
					msg = left(rs("msg"),25) & ".."
				end if
				if rs("flag") = "Y" then 
					vFlag = "진"
					span = "<span style=''>"
				elseif rs("flag")="C" then 
					vFlag = "완"
					span = "<span style='color:#AAA'>"
				end if   
				msg = span & "&nbsp; - " & msg & "(" & vFlag & ")</span><br>"
					
			ElseIf procType = "offwork" Then 
				if rs("workTm") < "80" then 
					msg = "&nbsp; - " & rs("msg") & "(" & rs("workTm") & "h)<br>"
				else
					if rs("workTm") = "88" then
						dFlag = "휴가"
					else
						dFlag = "정근"
					end if  
					msg = "&nbsp; - " & rs("msg") & "(" & dFlag & ")<br>"
				end if 
			ElseIf procType = "equip" Then 
				fullLotNo = rs("lotYear") & rs("lotNo")
				parmLotNo = rs("lotYear") & rs("lotNo") & "-0"
				If rs("lotSubNo") <> "0" Then 
					fullLotNo = fullLotNo & "-" & rs("lotSubNo")
					parmLotNo = fullLotNo
				End If 
				totalQty = (CDbl(rs("goodQty"))+CDbl(rs("badQty")))
				badRate = ""
				If  totalQty<> "0" Then 
					badRate = CDbl(rs("badQty"))/totalQty
					badRate = Round(badRate,3) * 100
					badRate = "(<font color='#ff0000'>" & badRate  & "%</font>)"
				End If 
				goodQty = rs("goodQty")
				badQty = "/" & rs("badQty")

				LColor = ""
				workDt = Replace(rs("workDt"),"-","")
				curDt = Replace(Date(),"-","")
				if rs("goodQty") = "0" And workDt<curDt then 
					LColor = "#ff9a02"
				end if 
				msg = "&nbsp; - <a href=""jobOrder.asp?fullLotNo=" & parmLotNo & """ title=""" & rs("prdNm") & """ target=""_blank"" style='color:" & LColor & "'>" & rs("msg") & " : " & goodQty  & badQty & " " & badRate & "</a><br>"
			Else
				fullLotNo = rs("lotYear") & rs("lotNo")
				parmLotNo = rs("lotYear") & rs("lotNo") & "-0"
				If rs("lotSubNo") <> "0" Then 
					fullLotNo = fullLotNo & "-" & rs("lotSubNo")
					parmLotNo = fullLotNo
				End If 
				'prdNm = Left(rs("prdNm"),21) & "..."
				totalQty = (CDbl(rs("goodQty"))+CDbl(rs("badQty")))
				badRate = ""
				If  totalQty<> "0" Then 
					badRate = CDbl(rs("badQty"))/totalQty
					badRate = Round(badRate,3) * 100
					badRate = "(<font color='#ff0000'>" & badRate  & "%</font>)"
				End If 
				goodQty = rs("goodQty")
				badQty = "/" & rs("badQty")
				If procType = "90" Or procType = "98" Then 
					badRate = ""
					badQty = ""
				End If 
				
				LColor = ""
				if rs("type") = "D" then 
					LColor = "#339900"
				end if 
				
				workDt = Replace(rs("workDt"),"-","")
				curDt = Replace(Date(),"-","")
				if rs("goodQty") = "0" And workDt<curDt then 
					LColor = "#ff9a02"
				end if 
				
				equip = ""
				if rs("etc") <> "" and procType = "10" then 
					'equip = "<br> &nbsp; &nbsp; " & rs("etc")
					equip = "[<b>" & left(rs("etc"),2) & "</b>] "
				else 
					equip = "&nbsp;- " 
				end if 
				msg = "&nbsp;<a href=""jobOrder.asp?fullLotNo=" & parmLotNo & """ title=""" & rs("prdNm") & """ target=""_blank"" style='color:" & LColor & "'>" & equip & fullLotNo & " : " & goodQty  & badQty & " " & badRate & "</a><br>"
			End If
			Call mkXMLNode(idNm, msg , 2)

		Rs.movenext
		Loop 
	End If
	Response.write "	</Work>" & vbcrlf
	Response.write "</Calendar>" & vbcrlf
%>